import { apiInit } from '@/services'
import { ref, Ref } from 'vue'
import { message } from 'ant-design-vue'
import { ColSize } from 'ant-design-vue/lib/grid/Col'
import router from '@/entry_desktop/router'
import { i18n } from '@/locales'
const { $t } = i18n
export default function useModel() {
	const api = apiInit()
	const processList: any = ref([])
	const getProcessList = async (name: string) => {
		const res = await api.delegateAuto.ListProcessDelegatePrivateV3Ego_apiPOST({
			payload: { name },
		})
		if (res.code === '000000') {
			processList.value = res.data ?? []
		} else {
			message.error(res.message ?? '')
		}
	}
	const addDelegate = async (
		params: any,
		isSysAdmin: boolean,
		notCurrent?: boolean
	) => {
		const {
			agentId,
			endDate,
			isAll,
			processKeyList,
			startDate,
			status,
			sendMsg,
			entrustedId,
		} = params
		let res = null
		let payload: any = {
			agent_id: agentId,
			end_date: endDate,
			is_all: isAll,
			process_key_list: isAll === '1' ? undefined : processKeyList,
			start_date: startDate,
			status,
			send_msg: Number(sendMsg),
			consigner_id: entrustedId,
		}
		const { start_dept_id, has_child } = params
		payload = { ...payload, start_dept_id, has_child }
		if (!isSysAdmin || !notCurrent) {
			res = await api.delegateAuto.AddDelegatePrivateV3Ego_apiPOST({ payload })
		} else {
			res = await api.delegateAuto.AdminAddDelegatePrivateV3Ego_apiPOST({
				payload,
			})
		}
		if (res.code === '000000') {
			message.success($t('delegate.actionSuccessful', '操作成功') ?? '')
			router.push({
				name: 'delegation',
				query: {
					tab: notCurrent ? 'creat_byother' : 'creat_byme',
				},
			})
			return true
		} else {
			message.warning(res.message ?? '')
			return true
		}
	}
	//修改
	const editDelegate = async (
		params: any,
		isSysAdmin: boolean,
		jumpPostion?: boolean
	) => {
		const {
			id,
			agentId,
			endDate,
			isAll,
			processKeyList,
			startDate,
			status,
			sendMsg,
			entrustedId,
		} = params
		let res = null
		let payload = {
			id: id,
			agent_id: agentId,
			end_date: endDate,
			is_all: isAll,
			process_key_list: processKeyList,
			start_date: startDate,
			status,
			send_msg: Number(sendMsg),
			consigner_id: entrustedId,
		}
		if (!isSysAdmin) {
			res = await api.delegateAuto.EditDelegatePrivateV3Ego_apiPOST({ payload })
		} else {
			const { start_dept_id, has_child } = params
			res = await api.delegateAuto.AdminEditDelegatePrivateV3Ego_apiPOST({
				payload: {
					...payload,
					start_dept_id: start_dept_id,
					has_child: has_child,
				},
			})
		}
		if (res.code === '000000') {
			message.success($t('delegate.actionSuccessful', '操作成功') ?? '')
			router.push({
				name: 'delegation',
				query: {
					tab: jumpPostion ? 'creat_byother' : 'creat_byme',
				},
			})
			return true
		} else {
			message.warning(res.message ?? '')
			return true
		}
	}
	//根据id获取委托任务详情
	const mandateForLegislator = async (
		id: string,
		joggle: boolean,
		status: any
	) => {
		let res = null
		if (!joggle) {
			res = await api.delegateAuto.GetDelegatePrivateV3Ego_apiPOST({
				payload: { id },
			})
		} else {
			res = await api.delegateAuto.GetAdminDelegatePrivateV3Ego_apiPOST({
				payload: { id, status },
			})
		}
		if (res.code === '000000') {
			const dataArray: any = (res.data as any)?.process_list
			return dataArray
		}
	}
	// 委托授权接口请求
	const getEmployeesInfo = async (value: string, ifSelf: boolean) => {
		const result: any = await api.default.GetEmployeesInfoUserPrivateV2POST({
			payload: {
				include_curr_user: ifSelf,
				param: value,
			},
		})
		return {
			code: '000000',
			data: [...result.data.rows],
		}
	}

	return {
		getProcessList,
		processList,
		addDelegate,
		editDelegate,
		mandateForLegislator,
		getEmployeesInfo,
	}
}
